package com.bainuo.doctor.common.widget.ptr;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;


public abstract class PtrDefaultHandler implements PtrHandler {

    public static boolean canChildScrollUp(View view) {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (view instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) view;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                        .getTop() < absListView.getPaddingTop());
            } else {
                return view.getScrollY() > 0;
            }
        } else {
            if (view instanceof ViewGroup) {
                ViewGroup group = (ViewGroup) view;
                if (view instanceof AbsListView) {
                    AbsListView absListView = (AbsListView) view;
                    return absListView.getChildCount() > 0
                            && (absListView.getFirstVisiblePosition() <= 0 && absListView.getChildAt(0)
                            .getTop() >= absListView.getPaddingTop());
                } else if (view instanceof RecyclerView) {
                    RecyclerView recyclerView = (RecyclerView) view;
                    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
                        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                        if (recyclerView.getChildCount() == 0) {
                            return view.getScrollY() <= 0;
                        }
                        return recyclerView.getChildCount() > 0 && (layoutManager.findFirstVisibleItemPosition() <= 0
                                && recyclerView.getChildAt(0).getTop() >= recyclerView.getPaddingTop());
                    } else {
                        GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
                        if (recyclerView.getChildCount() == 0) {
                            return view.getScrollY() <= 0;
                        }
                        return recyclerView.getChildCount() > 0 && (layoutManager.findFirstVisibleItemPosition() <= 0
                                && recyclerView.getChildAt(0).getTop() >= recyclerView.getPaddingTop());
                    }
                }
                // or interate the children
                for (int i = 0; i < group.getChildCount(); i++) {
                    if (group.getChildAt(i) instanceof AbsListView || group.getChildAt(i) instanceof RecyclerView) {
                        if (group.getChildAt(i) instanceof AbsListView) {
                            AbsListView absListView = (AbsListView) group.getChildAt(i);
                            if (absListView.getChildCount() == 0) return view.getScrollY() <= 0;
                            return absListView.getChildCount() > 0
                                    && (absListView.getFirstVisiblePosition() <= 0 && absListView.getChildAt(0)
                                    .getTop() >= absListView.getPaddingTop());
                        } else {
                            RecyclerView recyclerView = (RecyclerView) group.getChildAt(i);
                            if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
                                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                                if (recyclerView.getVisibility() == View.GONE) {
                                    continue;
                                }
                                if (recyclerView.getChildCount() == 0) {
                                    return view.getScrollY() <= 0;
                                }
                                return recyclerView.getChildCount() > 0 && (layoutManager.findFirstVisibleItemPosition() <= 0
                                        && recyclerView.getChildAt(0).getTop() >= recyclerView.getPaddingTop());
                            } else {
                                GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
                                if (recyclerView.getChildCount() == 0) {
                                    return view.getScrollY() <= 0;
                                }
                                return recyclerView.getChildCount() > 0 && (layoutManager.findFirstVisibleItemPosition() <= 0
                                        && recyclerView.getChildAt(0).getTop() >= recyclerView.getPaddingTop());
                            }
                        }
                    }
                }
            }
            return view.getScrollY() <= 0;
        }
    }

    /**
     * Default implement for check can perform pull to refresh
     *
     * @param frame
     * @param content
     * @param header
     * @return
     */
    public static boolean checkContentCanBePulledDown(PtrFrameLayout frame, View content, View header) {
        return canChildScrollUp(content);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return checkContentCanBePulledDown(frame, content, header);
    }
}